﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Domain.MainModule.SoundexCodes;
using Domain.Core;

namespace Application.MainModule.SoundexCodesManagement
{
    public class SoundexCodeManagementService: ISoundexCodeManagementService
    {
        #region Members

       ISoundexCodeRepository _SoundexCodeRepository;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor for SoundexCodeService
        /// </summary>
        /// <param name="soundexCodeRepository">SoundexCodeRepository dependency, usually this is resolver with IoC</param>
        public SoundexCodeManagementService(ISoundexCodeRepository soundexCodeRepository)
        {
            if (soundexCodeRepository == (ISoundexCodeRepository)null)
                throw new ArgumentNullException(Resources.Messages.exception_DependenciesAreNotInitialized);

            _SoundexCodeRepository = soundexCodeRepository;
        }

        #endregion

        public void AddSoundexCode(SoundexCode soundexCode)
        {
            if (soundexCode == null)
                throw new ArgumentNullException(Resources.Messages.exception_InvalidUserId, "soundexCode");

            IUnitOfWork unitOfWork = _SoundexCodeRepository.UnitOfWork as IUnitOfWork;
            _SoundexCodeRepository.Add(soundexCode);
            unitOfWork.Commit();

        }

        public void ChangeSoundexCode(SoundexCode soundexCode)
        {

            if (soundexCode == null)
                throw new ArgumentNullException(Resources.Messages.exception_InvalidUserId, "soundexCode");

            IUnitOfWork unitOfWork = _SoundexCodeRepository.UnitOfWork as IUnitOfWork;
            _SoundexCodeRepository.Modify(soundexCode);
            unitOfWork.CommitAndRefreshChanges();

        }

        public void RemoveSoundexCode(SoundexCode soundexCode)
        {
            if (soundexCode == null)
                throw new ArgumentNullException(Resources.Messages.exception_InvalidUserId, "soundexCode");

            IUnitOfWork unitOfWork = _SoundexCodeRepository.UnitOfWork as IUnitOfWork;
            _SoundexCodeRepository.Remove(soundexCode);
            unitOfWork.CommitAndRefreshChanges();
        }

        public List<SoundexCode> FindSoundexCodeByUserId(Guid userId)
        {
            SoundexCodeUserIdSpecification spec = new SoundexCodeUserIdSpecification(userId);
            return _SoundexCodeRepository.FindSoundexCode(spec).ToList<SoundexCode>();
        }

        public List<SoundexCode> FindSoundexCodeByValue(string value)
        {
            if(string.IsNullOrEmpty(value))
                throw new ArgumentNullException(Resources.Messages.exception_InvalidArgument, "value");
            SoundexCodeValueSpecification spec = new SoundexCodeValueSpecification(value);
            return _SoundexCodeRepository.FindSoundexCode(spec).ToList<SoundexCode>();
        }
    }
}
